package com.example.myapplication.Caringwelfare;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.example.myapplication.Config.TokenBean;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.CarouselMapBean;
import com.example.myapplication.ShuJu.GoodlistitemBean;
import com.example.myapplication.ShuJu.Hostel_DetailBean;
import com.example.myapplication.ShuJu.Re_Servlet;
import com.example.myapplication.ShuJu.WelfareclassificationBean;
import com.example.myapplication.apter.CaringwelfareViewPagerApter;
import com.example.myapplication.apter.CaringwelfarelistApter;
import com.example.myapplication.apter.PhotoViewPagerApter;
import com.example.myapplication.apter.ServerApter;
import com.example.myapplication.apter.WelfareclassificationApter;
import com.example.myapplication.teenager.Hostel_DetailActivity;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CaringwelfareActivity extends AppCompatActivity {

    private ViewPager re_caring;
    private  Gson gson ;
    private  Handler handler ;
    private  OkHttpClient okHttpClient ;
    private Request request ;
    private CarouselMapBean carouselMapBean ;
    private RecyclerView re_Classification ;
    private RecyclerView re_Recommended ;
    private WelfareclassificationBean welfareclassificationBean ;
    private GoodlistitemBean goodlistitemBean ;
    private EditText ed_input ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_caringwelfare);
        iniyview();
        //轮播图
        getCarouselMap();
        //公益分类
        welfareclassification();
        //推荐公益列表项
        goodlistitem();

    }

    private void goodlistitem() {
        request =  new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/public-welfare/public-welfare-activity/recommend-list")
                .addHeader("Authorization", TokenBean.token)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
                Log.d("xx","bb");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =  response.body().string();
                Log.d("xjson",json);
                gson = new Gson();
                goodlistitemBean = gson.fromJson(json, GoodlistitemBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run(){
                        Caringwelfarelistitem();
                    }
                });
            }
        });
    }

    private void  Caringwelfarelistitem() {
        CaringwelfarelistApter caringwelfarelistApter = new CaringwelfarelistApter(CaringwelfareActivity.this,goodlistitemBean.getRows());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(CaringwelfareActivity.this);
        re_Recommended.setLayoutManager(linearLayoutManager);
        re_Recommended.setAdapter(caringwelfarelistApter);

    }

    private void welfareclassification() {
        request =  new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/public-welfare/public-welfare-type/list")
                .addHeader("Authorization",TokenBean.token)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
                Log.d("xx","bb");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String json =  response.body().string();
                Log.d("xjson",json);
                gson = new Gson();
                 welfareclassificationBean = gson.fromJson(json, WelfareclassificationBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run(){
                        binServer();
                    }
                });
            }
        });
    }

    private void binServer() {
        //适配器
        //ridLayoutManager 是 Android 中的一种布局管理器，用于在 RecyclerView 中展示网格布局。它将项目以网格状的形式进行排列，并提供了灵活的跨行和跨列功能。
        WelfareclassificationApter welfareclassificationApter = new WelfareclassificationApter(CaringwelfareActivity.this,welfareclassificationBean.getData());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(CaringwelfareActivity.this,4);
        re_Classification.setLayoutManager(gridLayoutManager);
        re_Classification.setAdapter(welfareclassificationApter);
    }

    private void getCarouselMap() {
        request = new Request.Builder()
                .url("http://124.93.196.45:10001/prod-api/api/public-welfare/ad-banner/list")
                .addHeader("Authorization",TokenBean.token)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.d("hostel_detailBean","string============================");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                carouselMapBean = gson.fromJson(string, CarouselMapBean.class);
                Log.d("hostel_detailBean",string);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        CaringwelfareViewPagerApter caringwelfareViewPagerApter = new CaringwelfareViewPagerApter(CaringwelfareActivity.this,carouselMapBean.getData());
                        re_caring.setAdapter(caringwelfareViewPagerApter);
                        mHandler.sendEmptyMessageDelayed(0, 3 * 500);
                    }
                });
            }
        });
    }

    private void iniyview() {
        re_caring = findViewById(R.id.re_caring);
        ed_input = findViewById(R.id.ed_input);
        re_Classification = findViewById(R.id.re_Classification);
        re_Recommended = findViewById(R.id.re_Recommended);
        okHttpClient = new OkHttpClient();
        gson = new Gson();
        handler = new Handler();
    }
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (msg.what == 0) {
                int index = re_caring.getCurrentItem();
                index = (index + 1) % 5;
                re_caring.setCurrentItem(index);
                mHandler.sendEmptyMessageDelayed(0, 3 * 500);
            }
        }
    };
}